/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao;

import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @author SBH
 */
public class BookDao {
    // 成员变量定义

    private static int SUCCESS = 1;
    private static int ERROR = 2;
    private static int SAME = 3;
    // 记录集对象
    private ResultSet rs;
    // 获取书籍分类信息

    public List getFenLei() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from bookclassification order by bookclass_id desc");
            rs = ps.executeQuery();
            con.commit();
            while (rs.next()) {
                // 数组容器类对象-中间对象
                List list2 = new ArrayList();
                // 接收此书籍分类信息
                list2.add(rs.getString(1));
                list2.add(rs.getString(2));
                list2.add(rs.getString(3));
                // 将此书籍分类信息加入到书籍信息库
                list.add(list2);
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
            return list;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }

    public int addFenLei(String bookclass_classification) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();
        Random random = new Random();

        int r = random.nextInt(100);

        try {
            // 执行SQL查询语句
            rs = ps.executeQuery("select * from bookclassification where bookclass_classification='" + bookclass_classification + "'");
            con.commit();
            // 查看是否重名
            if (rs.next()) { // 有重名
                // 返回重名标记
                return this.SAME;
            } else { //无重名
                // 执行SQL更新操作-添加
                ps = con.prepareStatement("insert into bookclassification(bookclass_id,bookclass_classification,bookclass_addtime) values(?,?,?)");
                ps.setInt(1, r);
                ps.setString(2, bookclass_classification);
                ps.setString(3, new java.util.Date().toLocaleString());
                int i = ps.executeUpdate();

                if (i == 1) { //添加成功
                    // 返回执行成功标记
                    return this.SUCCESS;
                } else { //添加失败
                    // 返回执行失败标记
                    return this.ERROR;
                }
            }
        } catch (Exception e) { //捕获异常
            e.printStackTrace();
            // 返回执行失败标记
            return this.ERROR;
        } finally { // 最后一定执行代码
            // 关闭数据库
            DatabaseUtil.close(rs, ps, con);
        }

    }

    public int delFenLei(int id[]) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = DatabaseUtil.getConnection();
            con.commit();
            for (int j = 0; j < id.length; j++) {
                ps = con.prepareStatement("delete from bookclassification where bookclass_id=?");
                ps.setInt(1, id[j]);
                ps.executeUpdate();
            }
            return this.SUCCESS;

        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }

    public int delBook(int id[]) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int rows = 0;

        try {
            con = DatabaseUtil.getConnection();
            con.commit();
            for (int j = 0; j < id.length; j++) {
                ps = con.prepareStatement("delete from bookinformation where book_id=?");
                ps.setInt(1, id[j]);
                rows = ps.executeUpdate();
            }
            return this.SUCCESS;

        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }

    public int upBook(int book_id, String book_name, String book_author, String book_publisher, String book_price, String book_classification, String book_discountok, String book_discount, String book_recommendlevel, String book_storeamount) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            // 执行SQL语句
            con.commit();
            ps = con.prepareStatement("update bookinformation set book_name='" + book_name + "',book_author='" + book_author + "',book_publisher='" + book_publisher + "',book_price='" + book_price + "',book_classification='" + book_classification + "',book_discountok='" + book_discountok + "',book_discount='" + book_discount + "',book_recommendlevel='" + book_recommendlevel + "',book_storeamount='" + book_storeamount + "'where book_id='" + book_id + "'");

            int i = ps.executeUpdate();
            if (i == 1) { //执行成功
                // 返回执行成功标记
                return this.SUCCESS;
            } else { //执行失败
                // 返回执行失败标记
                return this.ERROR;
            }
        } catch (Exception e) { //捕获异常
            e.printStackTrace();
            // 返回执行失败标记
            return this.ERROR;
        } finally { // 最后一定执行代码
            // 关闭数据库
            DatabaseUtil.close(rs, ps, con);
        }

    }

}
